import { _decorator, Component, Node } from 'cc';
import { GamesManager } from '../../../modules/games/GamesManager';
import { instantiate } from 'cc';
import { Toggle } from 'cc';
import { ToggleContainer } from 'cc';
import { Global } from '../../..//script/Global';
import { RoomProto } from '../../room/script/RoomProto';
import { ICS_ROOM_CREATE, ISC_ROOM_CREATE } from 'db://assets/proto/proto';
import { RoomConst, RoomHint } from '../../room/script/RoomConst';
import { show_hint } from 'db://assets/sgame/ui/SUI';
import { SceneManager } from 'db://assets/script/scene/SceneManager';
const { ccclass, property } = _decorator;

@ccclass('CreatePanel')
export class CreatePanel extends Component {
    start() {
        let contentNode: Node = this.node.Child("gameList/view/content");
        let gameSample = contentNode.Child("sample");
        let availableGames = GamesManager.AvailableGames(true);
        let toogleContainer: ToggleContainer = contentNode.Component(ToggleContainer);
        for (const sort in availableGames) {
            let game = availableGames[sort];
            let gameNode = instantiate(gameSample);
            gameNode.name = game.name;
            gameNode.setParent(contentNode);
            gameNode.LabelChild("name", game.cnName);
            gameNode.active = true;
        }

        if (availableGames[0]) {
            contentNode.Component(availableGames[0].name, Toggle).isChecked = true;
        }

        this.node.OnClickChild("create", () => {
            let gameName = toogleContainer.activeToggles()[0].node.name;
            let params: ICS_ROOM_CREATE = {
                gameName: gameName,
                // rule: {}
            }

            Global.gameSocket.request(RoomProto.ROOM_CREATE, params, (result: ISC_ROOM_CREATE) => {
                if (result.code != RoomConst.CREATE_RESULT_CODE.SUCCESS) {
                    show_hint(`创建失败, ${RoomHint.CREATE_RESUT[result.code]}`);
                    return;
                }

                show_hint(`创建成功, ${RoomHint.CREATE_RESUT[result.code]}`);
                console.log(result);
                SceneManager.instance().enter(gameName);
            });
        })
    }
}
